package com.cskaoyan.controller;

import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.bean.basebean.BaseRespVo;
import com.cskaoyan.bean.basebean.BasePageData;
import com.cskaoyan.bean.Coupon;
import com.cskaoyan.exception.CouponException;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.CouponUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lxy
 * @create 2021-10-18 19:00
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;

    @RequiresPermissions(value = "admin:coupon:list")
    @RequestMapping("list")
    public BaseRespVo list(String name, Short type, Short status, BaseParam baseParam) {
        BasePageData data = couponService.query(name, type, status, baseParam);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions(value = "admin:coupon:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Coupon coupon) throws CouponException {
        couponService.deleteCoupon(coupon);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = "admin:coupon:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody Coupon coupon) throws CouponException {
        couponService.createCoupon(coupon);
        return BaseRespVo.ok(coupon);
    }

    @RequiresPermissions(value = "admin:coupon:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Coupon coupon) throws CouponException {
        couponService.updateCoupon(coupon);

        return BaseRespVo.ok(coupon);
    }

    @RequiresPermissions(value = "admin:coupon:read")
    @RequestMapping("read")
    public BaseRespVo read(Integer id) {
        Coupon coupon = couponService.readCoupon(id);
        return BaseRespVo.ok(coupon);
    }

    @Autowired
    CouponUserService couponUserService;

    @RequiresPermissions(value = "admin:coupon:listuser")
    @RequestMapping("listuser")
    public BaseRespVo listUser(Integer couponId, Integer userId, Short status, BaseParam baseParam) {
        BasePageData data = couponUserService.queryUser(couponId, userId, status, baseParam);
        return BaseRespVo.ok(data);
    }
}
